﻿Imports System.Data.SqlClient
Public Class Login
    Public cm As SqlCommand

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        txtusername.Focus()
        txtusername.SelectAll()
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' If e.KeyCode = Keys.Enter Then
        Try
            Main.Label1.Text = txtusername.Text
            cm = New SqlCommand
            cm.Connection = cn
            cm.CommandText = "select Username,Password from tblUser where Username=N'" & txtusername.Text & "' and Password=N'" & txtpassword.Text & "'"
            Dim reader As SqlDataReader
            reader = cm.ExecuteReader
            If reader.HasRows Then
                home.Show()
            Else

                MessageBox.Show("Username and Password incorrect", "", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

            reader.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

        ' for user and admin    
        If txtusertype.Text = "User" Then
            home.btnemployee.Enabled = False
            home.btncategory.Enabled = False
            home.btnchangmoney.Enabled = False
            home.btnuser.Enabled = False
            home.btnstatistic.Enabled = False

        Else
            ' Full Contral
        End If
    End Sub

    Private Sub txtusername_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtusername.TextChanged
        condb()
        Dim cmd As New SqlCommand("select usertype from tbluser where Username='" & txtusername.Text & "'", cn)
        txtusertype.Text = cmd.ExecuteScalar
    End Sub

    Private Sub txtpassword_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtpassword.KeyDown
        If e.KeyCode = Keys.Enter Then
            Try
                Main.Label1.Text = txtusername.Text
                cm = New SqlCommand
                cm.Connection = cn
                cm.CommandText = "select Username,Password from tblUser where Username=N'" & txtusername.Text & "' and Password=N'" & txtpassword.Text & "'"
                Dim reader As SqlDataReader
                reader = cm.ExecuteReader
                If reader.HasRows Then
                    home.Show()
                Else

                    MessageBox.Show("Username and Password incorrect", "", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

                reader.Close()
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try
        End If

        ' for user and admin    
        If txtusertype.Text = "User" Then
            home.btnemployee.Enabled = False
            home.btncategory.Enabled = False
            home.btnchangmoney.Enabled = False
            home.btnuser.Enabled = False
            home.btnstatistic.Enabled = False

        Else
            ' Full Contral
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class